package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CallBatchCallConsumeDTO;
import com.xbongbong.paas.pojo.dto.CallBatchCallRechargeDTO;
import com.xbongbong.paas.pojo.dto.CallGetNotEndCallDTO;
import com.xbongbong.paas.pojo.dto.CallTaskAgainDTO;
import com.xbongbong.paas.pojo.dto.CallTaskListDTO;
import com.xbongbong.paas.pojo.dto.CallTaskSaveDTO;
import com.xbongbong.paas.pojo.vo.CallBatchCallConsumeVO;
import com.xbongbong.paas.pojo.vo.CallBatchCallRechargeVO;
import com.xbongbong.paas.pojo.vo.CallGetNotEndCallVO;
import com.xbongbong.paas.pojo.vo.CallTaskAgainVO;
import com.xbongbong.paas.pojo.vo.CallTaskListVO;
import com.xbongbong.paas.pojo.vo.CallTaskSaveVO;
import com.xbongbong.paas.service.XbbCallTaskService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 批量外呼计划接口
 * @date 2019/2/22 15:42
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/callCenter/XbbCall")
public class XbbCallTaskController {

    @Resource
    private XbbCallTaskService xbbCallTaskService;

    /**
     * 保存批量外呼计划
     *
     * @return 保存批量外呼计划
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callTask/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callTaskSave(@RequestBody @Valid CallTaskSaveDTO callTaskSaveDTO, BindingResult br) {
        XbbResponse<CallTaskSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskService.callTaskSave(callTaskSaveDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 批量外呼计划列表
     *
     * @return 批量外呼计划列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callTask/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callTaskList(@RequestBody @Valid CallTaskListDTO callTaskListDTO, BindingResult br) {
        XbbResponse<CallTaskListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskService.callTaskList(callTaskListDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 查看上次是否有未完成的批量外呼进度
     *
     * @return 查看上次是否有未完成的批量外呼进度
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/getNotEndCall", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getNotEndCall(@RequestBody @Valid CallGetNotEndCallDTO callGetNotEndCallDTO, BindingResult br) {
        XbbResponse<CallGetNotEndCallVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskService.getNotEndCall(callGetNotEndCallDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 批量外呼消费明细列表
     *
     * @return 批量外呼消费明细列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/batchCallConsume", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String batchCallConsume(@RequestBody @Valid CallBatchCallConsumeDTO callBatchCallConsumeDTO, BindingResult br) {
        XbbResponse<CallBatchCallConsumeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskService.batchCallConsume(callBatchCallConsumeDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 批量外呼充值明细列表
     *
     * @return 批量外呼充值明细列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/batchCallRecharge", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String batchCallRecharge(@RequestBody @Valid CallBatchCallRechargeDTO callBatchCallRechargeDTO, BindingResult br) {
        XbbResponse<CallBatchCallRechargeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskService.batchCallRecharge(callBatchCallRechargeDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 再次智能批量外呼计划
     *
     * @return 再次智能批量外呼计划
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callTask/again", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callTaskAgain(@RequestBody @Valid CallTaskAgainDTO callTaskAgainDTO, BindingResult br) {
        XbbResponse<CallTaskAgainVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.xbbCallTaskService.callTaskAgain(callTaskAgainDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
